package gspushmq.v1.app.subscribeapp.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import gspushmq.v1.app.subscribeapp.enumeration.ExitEnum;
import gspushmq.v1.app.subscribeapp.enumeration.LinkEnum;
import gspushmq.v1.app.subscribeapp.enumeration.PlatformEnum;
import gspushmq.v1.app.subscribeapp.enumeration.ProducerEnum;
import gspushmq.v1.app.subscribeapp.enumeration.ProductLineEnum;
import gspushmq.v1.app.subscribeapp.enumeration.PushTypeEnum;
import gspushmq.v1.app.subscribeapp.enumeration.TagEnum;
import gspushmq.v1.app.subscribeapp.enumeration.TagNoPushEnum;
import gspushmq.v1.app.subscribeapp.enumeration.TargetEnum;

/**
 * @Description 本地库中消息对象结构
 * @Author chengsj
 * @Date 2018/9/21 11:43
 * @Version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2015 SunTime Co'Ltd Inc. All rights reserved.
 **/
public class LocalDO extends BaseVO implements Serializable {

    private Long id;
    private String sid;
    private String plate_name;
    private String source_name;
    private String consumer;
    private String producer;
    private String message_type_name;
    private String target;
    private String target_value;
    private String target_name;
    private String tags_group;
    private Integer push_status;
    private String push_status_name;
    private String notice_link_name;
    private Integer status = ExitEnum.EXIT.getCode();
    private String status_name = ExitEnum.EXIT.getValue();
    private Date push_time;
    private Date insert_time;
    private Date update_time;
    public LocalDO() { }

    public LocalDO(BaseVO vo){
        super(vo);
        setSource_name(super.getSource());
        setTarget(vo.getTarget_code());
        setTarget_value(super.getLevels(),vo.getTarget_code(),vo.getProducer_code());
        setTarget_name(super.getTarget_code());
        setTags_group(super.getLevels(),vo.getTarget_code());
        setNotice_link_name(super.getNotice_link_type());
        setProducer(super.getProducer_code());
        setPlate_name(super.getPlate_code());
    }


    public String getMessage_type_name() {
        return message_type_name;
    }

    public void setMessage_type_name(String message_type_name) {
        this.message_type_name = message_type_name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNotice_link_name() {
        return notice_link_name;
    }

    public void setNotice_link_name(Integer notice_link_type) {
        this.notice_link_name = LinkEnum.getValueByCode(notice_link_type);
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(Integer producer_code) {
        this.producer = ProducerEnum.getNameByCode(producer_code);
        this.message_type_name = ProducerEnum.getMnameByCode(producer_code);
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getPlate_name() {
        return plate_name;
    }

    public void setPlate_name(Integer  plate_code) {
        this.plate_name = PlatformEnum.getNameByCode(plate_code);
    }

    public String getSource_name() {
        return source_name;
    }
    public void setSource_name(Integer source) {
        this.source_name = ProductLineEnum.fromProductName(source);
    }
    public String getConsumer() {
        return consumer;
    }

    public void setConsumer(String consumer ) {
        this.consumer = consumer;
    }
    public String getTarget() {
        return target;
    }

    public void setTarget(Integer target_code) {
        if (PushTypeEnum.GROUP.getCode() == target_code) {
            this.target =  TargetEnum.TAG.getKey();
        }else if (PushTypeEnum.USERS.getCode() == target_code){
            this.target = TargetEnum.ACCOUNT.getKey();
        }
    }

    public String getTarget_value() {
        return target_value;
    }





    public void setTarget_value(String levels,Integer vo,Integer pro) {
        this.target_value =getTags(levels,vo,pro);
    }
    public void setTarget_value(String target_value){
        this.target_value = target_value;
    }
    private static String getTags(String levels,Integer prcode,Integer pro){
        if (StringUtils.isBlank(levels)) {
            return null;
        }
        if (PushTypeEnum.GROUP.getCode() == prcode){
            List<Integer> levs = new ArrayList<>();
            if (levels.contains(",")) {
                String[] les = levels.split(",");
                if (les != null) {
                    for (String s : les) {
                        levs.add(Integer.valueOf(s));
                    }
                }
            }else {
                levs.add(Integer.valueOf(levels));
            }
            if (levs.size() > 0){
                StringBuffer tags = new StringBuffer("{\"and\":[");
                //not
                if (ProducerEnum.GG_BACK.getCode() == pro){
                    tags.append("{ \"not\":{\"tag\":\"").append(TagNoPushEnum.NO_ASSISTANT.getTag()).append("\"}},");
                }
                if (ProducerEnum.GG_BACK.getCode() == pro){
                    tags.append("{ \"not\":{\"tag\":\"").append(TagNoPushEnum.NO_LIVEREMINDER.getTag()).append("\"}},");
                }
                if (ProducerEnum.GG_BACK.getCode() == pro){
                    tags.append("{ \"not\":{\"tag\":\"").append(TagNoPushEnum.NO_OWNCHOOSECHANGE.getTag()).append("\"}},");
                }
                //or
                tags.append("{\"or\":[");
                for (Integer i : levs){
                    tags.append("{\"tag\":\"").append(TagEnum.getTagByCode(i)).append("\"}");
                    if (!i .equals( levs.get(levs.size() - 1)))
                        tags.append(",");
                }
                tags.append("]}]}");
                return tags.toString();
            }
        }
        return levels;
    }
    public void setTarget_name(Integer target_code) {
        this.target_name = PushTypeEnum.getNameByCode(target_code);
    }
    public void setTags_group(String levels,Integer vo) {
        this.tags_group = getGroups(levels,vo);
    }
    private static String getGroups(String levels,Integer vo){
        if (StringUtils.isBlank(levels)) {
            return null;
        }
        if (PushTypeEnum.GROUP.getCode() == vo) {
            String[] les = levels.split(",");
            Set<Integer> levs = new HashSet<>();
            if (les!=null) {
                for (String s : les) {
                    levs.add(Integer.valueOf(s));
                }
            }
            if (levs.size() > 0) {
                StringBuffer tags = new StringBuffer();
                for (Integer i : levs) {
                    tags.append(TagEnum.getByNameCode(i)).append(",");
                }
                String str = tags.toString();
                if (str.endsWith(",")) {
                    str = str.substring(0, str.length() - 2);
                }
                return str;
            }
        }
        return levels;
    }





    public String getTags_group() {
        return tags_group;
    }

    public Integer getPush_status() {
        return push_status;
    }

    public String getTarget_name() {
        return target_name;
    }

    public void setPush_status(Integer push_status) {
        this.push_status = push_status;
    }

    public String getPush_status_name() {
        return push_status_name;
    }

    public void setPush_status_name(String push_status_name) {
        this.push_status_name = push_status_name;
    }


    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getStatus_name() {
        return status_name;
    }

    public void setStatus_name(String status_name) {
        this.status_name = status_name;
    }

    public Date getPush_time() {
        return push_time;
    }

    public void setPush_time(Date push_time) {
        this.push_time = push_time;
    }

    public Date getInsert_time() {
        return insert_time;
    }

    public void setInsert_time(Date insert_time) {
        this.insert_time = insert_time;
    }

    public Date getUpdate_time() {
        return update_time;
    }

    public void setUpdate_time(Date update_time) {
        this.update_time = update_time;
    }
}
